<template>
  <el-dialog
    draggable
    overflow
    :title="titleMap[mode]"
    v-model="visible"
    :width="400"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-tabs type="border-card">
      <el-tab-pane ref="tab" label="信息">
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="70px"
          label-position="right"
        >
          <el-row justify="space-evenly" :gutter="1" align="bottom">
            <el-col :span="20">
              <el-form-item label="分组" prop="parent_id">
                <sk-select-group
                  v-model="form.parent_id"
                  :list="categoryBuf"
                  :selectall="true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="是否大类" prop="lcategory_id">
                <skDicSelect
                  v-model="this.form.lcategory_id"
                  :code="'yesorno'"
                ></skDicSelect>
              </el-form-item>
            </el-col>

            <el-col :span="20">
              <el-form-item label="性质" prop="material_nature_id">
                <skDicSelect
                  v-model="this.form.material_nature_id"
                  :code="'MaterialNature'"
                ></skDicSelect>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
//

import skDicSelect from "@/components/skDicSelect";

import skSelectGroup from "@/components/skSelectGroup";
export default {
  emits: ["success", "closed", "update:modeValue"],
  components: {
    skSelectGroup,
    skDicSelect,
  },
  data() {
    return {
      mode: "add",
      titleMap: {
        add: "新增分组",
        edit: "编辑分组",
      },

      category: [],
      categoryBuf: [],
      form: {
        id: "",
        parent_id: "",
        name: "",
        material_nature_id: "",
        lcategory_id: 0,
        remark: "",
        sort: 0,
      },
      rules: {
        name: [{ required: true, message: "请输入名称" }],
        parent_id: [{ required: true, message: "请选择分组" }],
        material_nature_id: [{ required: true, message: "性质必选" }],
      },
      visible: false,
      isSaveing: false,
    };
  },
  mounted() {
    this.getCateGory();
  },
  methods: {
    //显示
    close() {
      this.visible = false;
      this.$emit("update:modeValue", false);
      this.$emit("closed");
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;

      return this;
    },

    async getCateGory(query = "全部物料") {
      if (this.category == "") {
        this.category = await this.$API.basedata.material.category.list.get(query);
      }
      this.categoryBuf = this.category.data.filter((item) => {
        return item.name.toLowerCase().includes(query.toLowerCase());
      });
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;

          var res = await this.$API.basedata.material.category.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          } else {
            this.$alert(res.message, "提示", { type: "error" });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      if (data.department == null) delete data.department;
      Object.assign(this.form, data);
    },
  },
};
</script>

<style></style>
